class LoginController < ApplicationController
  layout "login"

  def index

  end

  def process_login
        if user = User.authenticate(params[:user])
           session[:user] = user # Remember the user's id during this session
          #redirect_to session[:return_to] || '/'
           redirect_to :action => 'account', :name => params[:user][:name]
        else
           flash[:error] = 'Invalid login.'
           redirect_to :action => 'index'
        end
  end

  def logout
        reset_session
        flash[:message] = 'Logged out.'
        redirect_to :action => 'index', :controller=>'login'
  end

  def account

    @user_advertisments           = Advertisment.find(:all,:conditions=>'user_id='+session[:user][:id].to_s,:order=>"created_at desc",:limit=>2)
    @advertisment_responses       = Advertisment.find(:all,:conditions=>'user_id='+session[:user][:id].to_s,:order=>"created_at desc",:limit=>4)
    @user_advertisments_relevant  = Advertisment.find(:all,:order=>"created_at desc",:limit=>1)
    @user                         = Advertisment.find_by_user_id(session[:user][:id].to_s)
    @university_id                = @user.university_id
    @university_name              = University.find(@university_id).name
    @campus_advertisments         = Advertisment.find(:all,:conditions=>'university_id='+@university_id.to_s,:order=>"created_at desc",:limit=>2)
    @saved_advertisments          = BookmarkedAd.find(:all,:conditions=>'user_id = '+session[:user][:id].to_s,:order=>"created_at desc",:limit=>2)
  end
  def saved_ads
        @saved_advertisments      = BookmarkedAd.find(:all,:conditions=>'user_id ='+session[:user][:id].to_s,:order=>"created_at desc")
        render :layout=>false
  end
  def recently_posted_ads
     @user_advertisments          = Advertisment.paginate :page => params[:page],:per_page => 10,:conditions=>'user_id='+session[:user][:id].to_s,:order=>"created_at desc"
     @user_advertisments_relevant = Advertisment.find(:all,:order=>"created_at desc",:limit=>1)
       respond_to do |format|
          format.html
          format.js {
            render :update do |page|
            page.replace_html 'user-accounts-dashboard', :partial => 'recently_posted_ads'
            end
                    }
       end
  end
      def recent_responses
       @advertisment_responses  = Advertisment.find(:all,:conditions=>'user_id='+session[:user][:id].to_s,:order=>"created_at desc")
       render :layout=>false
      end
      def my_info
       @user                    = User.find(session[:user][:id])
       @user_type               = UserType.find(@user.user_type_id)
       @user_types              = UserType.find(:all, :include => [:categories],:conditions=>"id!="+@user.user_type_id.to_s)
       render :layout=> false
      end
  def edit_ad
        @advertisment           = Advertisment.find(params[:id])
        @sub_category           = SubCategory.find(@advertisment.sub_category_id)
    if request.post?
        Advertisment.update(params[:id], :title=> params[:edit_advertisment][:title], :description=> params[:edit_advertisment][:description],:location=> params[:edit_advertisment][:location])
        @advertisment_addx_table = AdvertismentAttributeFieldValue.find_by_advertisment_id(params[:id])
        AdvertismentAttributeFieldValue.update(@advertisment_addx_table.id,:field_values=>params[:advertisment_attributes])
        redirect_to :action => "recently_posted_ads"
    end
    
        render  :layout=>false
  end
  def show_advertisment
        @advertisment           = Advertisment.find(params[:id])
        @university             = @advertisment.university
        @user                   = @advertisment.user
        @similarads             = @advertisment.sub_category.advertisments.find(:all, :limit => 3)
        @sub_category_id        = Advertisment.find(params[:id]).sub_category_id
        @sub_category_lifespan  = SubCategory.find(@sub_category_id).lifespan
        @category_id            = SubCategory.find(@sub_category_id).category_id
        @group_id               = Category.find(@category_id).group_id
        @opposite_category_ids  = Category.find(:all,:conditions=>"id!="+@category_id.to_s+ " and group_id="+@group_id.to_s)
        @advertisment_attribute = @advertisment.advertisment_attribute_field_value
        unless @advertisment_attribute.nil?
         @field_values          = (@advertisment_attribute.field_values)
        end
        @images                 = @advertisment.attachments
        @firstimage             = @images.first
        @imagescount            = @images.length
        @responses=Query.count(:conditions=>"advertisment_id="+@advertisment.id.to_s,:order=>"created_at desc")
        render :layout=>false
  end

  def comments
      @advertisment             = Advertisment.find(params[:id])
      @comments                 = @advertisment.comments(:order=>"created_at")
      render :layout => false
  end

  def delete_advertisment
        Advertisment.delete(params[:advertisment])
        redirect_to :action => "recently_posted_ads"
  end

  def delete_comment
        Comment.delete(params[:comment])
        redirect_to :action => "recently_posted_ads"
  end
  def relevant_advertisments
        @advertisment           = Advertisment.find(params[:id])
        @university             = @advertisment.university
        @user                   = @advertisment.user
        @sub_category_id        = Advertisment.find(params[:id]).sub_category_id
        @sub_category_lifespan  = SubCategory.find(@sub_category_id).lifespan
        @category_id            = SubCategory.find(@sub_category_id).category_id
        @group_id               = Category.find(@category_id).group_id
        @opposite_category_ids  = Category.find(:all,:conditions=>"id!="+@category_id.to_s+ " and group_id="+@group_id.to_s)
        render :layout=>false
       
  end

  def renew_advertisment
        @advertisment           = Advertisment.find(params[:id])
        @sub_category_lifespan  = @advertisment.sub_category.lifespan
        @expires= Date.today + @sub_category_lifespan.days
        Advertisment.update(@advertisment,:expires_at=> @expires.to_formatted_s(:long_ordinal))
        render :layout=>false
  end
   def delete_renew_multiple_advertisments

   end

  def sort_by_responses
        @advertisments           = Advertisment.find(:all,:conditions=>"user_id="+ session[:user][:id].id.to_s)
       # @advertisment          = Query.find_by_advertisment_id(@advertisments)
#        @advertisment          = Query.find(:all,:conditions=>"advertisment_id="+@advertisment.id.to_s )
#        @advertisment          = Advertisment.find(:all,:conditions=>"id="+@advertisment.id.to_s)
        @user_advertisments     = @advertisments.paginate :page => params[:page],:per_page => 10,:conditions=>'advertisment_id='+@query.to_s,:order=>"created_at desc"
    
#       respond_to do |format|
#          format.html
#          format.js {
#            render :update do |page|
#            page.replace_html 'user-accounts-dashboard', :partial => 'recently_posted_ads'
#            end
#                    }
#       end
     render :layout=>false
  end
end


